<?php

namespace app\admin\controller;

use app\model\InsurantModel;
use app\model\PolicyModel;
use think\Db;
use think\Exception;
use think\Validate;

/**
 * Desc: 物权人
 * Class Insurant
 * Created By Doyi
 * Date: 2018/11/26
 */

class InsurantMan extends Base
{
    protected $model;

    public function __construct(PolicyModel $policyModel)
    {
        parent::__construct();
        $this->model = $policyModel;
    }

    /**
     * 列表展示
     * @return \think\response\View
     */
    public function index()
    {
        $url = url("InsurantMan/insurantList");
        $this->assign('url',$url);
        return $this->fetch('insurantMan/index');
    }

    /**
     * 列表接口
     */
    public function insurantList()
    {
        $param = $this->request->param();
        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;
        $arr = ['like'=>'p.policy_num'];
        $where = $this->select_param($arr,$param);
        $where['p.policy_status'] = 5;
        $list =  $this->model->getinsurantManListPage($page,$pageSize,$where,false);
        $count =  $this->model->getinsurantManListPage(0,0,$where,true);
        return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
    }






    public function stepList()
    {
        $url = url("InsurantMan/insurantStepList");
        $this->assign('url',$url);
        return $this->fetch('insurantMan/step_list');
    }

    public function insurantStepList()
    {
        $param = $this->request->param();
        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;
        $arr = ['like'=>'p.policy_num'];
        $where = $this->select_param($arr,$param);
        if(isset($param['status']) && !empty($param['status']) ){
            $where['i.status'] = $param['status'];
        }else{
            $where['i.status'] = 1 ;
        }
        $InsurantModel = new InsurantModel();
        $list =  $InsurantModel->getinsurantStepListPage($page,$pageSize,$where,false);
        foreach ($list as $k=>$v){
            if($list[$k]['entrust_url']){
                $list[$k]['entrust_url'] = json_decode($list[$k]['entrust_url']);
            }
        }
        $count =  $InsurantModel->getinsurantStepListPage(0,0,$where,true);
        return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
    }



    public function step_info()
    {
        $param = $this->request->param();
        $info = Db::table('policy')
            ->where('policy.policy_num',$param['policy_num'])
            ->field('policy.insurance_type,policy.report_num,policy.policy_num,policy.p_id')
            ->find();
        $info['insurance_type'] = explode(',',$info['insurance_type']);
        $str = [];
        foreach($info['insurance_type'] as $k =>$v){
            $res = Db::table('insurance_type')->where('it_id',$v)->find();
            $str[] =$res['it_name'];
        }
        $info['insurant_type_name'] = $str;
        $res = Db::table('insurant')->where('p_id',$info['p_id'])->find();
        if($res['status'] == 3){
            $where = [
                'i_id'=>$res['i_id'],
                'status'=>1,
            ];
            $returnMsg = Db::table('return_insurant')->where($where)->select();
            $data = [];
            foreach ($returnMsg as $k=>$v){
                $str = '&nbsp;&nbsp;&nbsp;&nbsp;';
                if($v['form_table'] == 'user_info'){
                    $user =  Db::table('user_info')->where('ui_id',$v['auth_id'])->find();
                    $name = $user['username'].'(业务员)';
                }else{
                    $user =  Db::table('admin')->where('id',$v['auth_id'])->find();
                    $name = $user['admin_name'].'(后台人员)';
                }
                if(!empty($v['join_price'])){
                    $data['join_price'][] = '驳回人员:'.$str.$name.$str.'驳回信息:'.$str.$v['join_price'];
                }
                if(!empty($v['look_goods_info'])){
                    $data['look_goods_info'][] ='驳回人员:'.$str.$name.$str.'驳回信息:'.$str.$v['look_goods_info'];
                }
                if(!empty($v['bol_info'])){
                    $data['bol_info'][] = '驳回人员:'.$str.$name.$str.'驳回信息:'.$str.$v['bol_info'];
                }
                if(!empty($v['receive_money'])){
                    $data['receive_money'][] = '驳回人员:'.$str.$name.$str.'驳回信息:'.$str.$v['receive_money'];
                }
                if(!empty($v['auth_report'])){
                    $data['auth_report'][] = '驳回人员:'.$str.$name.$str.'驳回信息:'.$str.$v['auth_report'];
                }
            }
            $this->assign('data',$data);
        }
        if($res['entrust_url']){
            $res['entrust_url'] = json_decode( $res['entrust_url']);
        }
        $this->assign('info',$info);
        $this->assign('res',$res);
        return $this->fetch('insurantMan/step_info');
    }


    public function save_return()
    {
        $param = $this->request->param();
        $this->assign('i_id',$param['i_id']);
        $this->assign('key',$param['key']);
        return $this->fetch('insurantMan/save_return');
    }

    public function save()
    {
        $param = $this->request->param();
        $key = $param['key'];
        $content = $param['content'];
        $i_id = $param['i_id'];
        $where = [
            'auth_id'=> $this->adminInfo['id'],
            'form_table'=> 'admin',
            'status'=>1
        ];
        $data = [
            $key => $content,
            'i_id' =>$i_id
        ];
        $res = Db::table('return_insurant')->where($where)->find();
        if($res){
            try{
                Db::startTrans();
                Db::table('return_insurant')->where($where)->update($data);
                Db::table('insurant')->where('i_id',$i_id)->update(['status'=>3,'update_at'=>date('Y-m-d H:i:s',time())]);
                Db::commit();
                return json(['code'=>200,'msg'=>'保存成功']);
            }catch (Exception $e){
                Db::rollback();
                return json(['code'=>400,'msg'=>'保存失败']);
            }
        }else{
            try{
                Db::startTrans();
                Db::table('return_insurant')->insert(array_merge($data,$where));
                Db::table('insurant')->where('i_id',$i_id)->update(['status'=>3,'update_at'=>date('Y-m-d H:i:s',time())]);
                Db::commit();
                return json(['code'=>200,'msg'=>'保存成功']);
            }catch (Exception $e){
                Db::rollback();
                return json(['code'=>400,'msg'=>'保存失败']);
            }
        }
    }

    /**
     * @return \think\response\Json
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public function pass()
    {
        $param = $this->request->param();
        $res = Db::table('insurant')->where('i_id',$param['i_id'])->update(['status'=>4]);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功,请在已完成当中查看']);
        }else{
            return json(['code'=>400,'msg'=>'操作失败']);
        }
    }




}